package com.moxiao.yfjh.module.activity.service.merchanType;

import com.moxiao.yfjh.module.activity.dal.dataobject.activityInfo.ActivityInfoDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.activity.controller.admin.merchanType.vo.*;
import com.moxiao.yfjh.module.activity.dal.dataobject.merchanType.MerchantTypeDO;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import com.moxiao.yfjh.module.activity.convert.merchanType.MerchantTypeConvert;
import com.moxiao.yfjh.module.activity.dal.mysql.merchanType.MerchantTypeMapper;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.activity.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲商家类型 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class MerchantTypeServiceImpl implements MerchantTypeService {

    @Resource
    private MerchantTypeMapper merchantTypeMapper;

    @Override
    public Long createMerchantType(MerchantTypeCreateReqVO createReqVO) {
        // 插入
        MerchantTypeDO merchantType = MerchantTypeConvert.INSTANCE.convert(createReqVO);
        merchantTypeMapper.insert(merchantType);
        // 返回
        return merchantType.getId();
    }

    @Override
    public void updateMerchantType(MerchantTypeUpdateReqVO updateReqVO) {
        // 校验存在
        validateMerchantTypeExists(updateReqVO.getId());
        // 更新
        MerchantTypeDO updateObj = MerchantTypeConvert.INSTANCE.convert(updateReqVO);
        merchantTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteMerchantType(Long id) {
        // 校验存在
        validateMerchantTypeExists(id);
        // 删除
        merchantTypeMapper.deleteById(id);
    }

    private void validateMerchantTypeExists(Long id) {
        if (merchantTypeMapper.selectById(id) == null) {
            throw exception(MERCHANT_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public MerchantTypeDO getMerchantType(Long id) {
        return merchantTypeMapper.selectById(id);
    }

    @Override
    public List<MerchantTypeDO> getMerchantTypeList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return merchantTypeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MerchantTypeDO> getMerchantTypePage(MerchantTypePageReqVO pageReqVO) {
        return merchantTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MerchantTypeDO> getMerchantTypeList(MerchantTypeExportReqVO exportReqVO) {
        return merchantTypeMapper.selectList(exportReqVO);
    }

    @Override
    public MerchantTypeDO getMerchantTypeTodayLast(Date todayStart, Date todayEnd) {
        LambdaQueryWrapper<MerchantTypeDO> lqw = Wrappers.lambdaQuery();
        if (Objects.nonNull(todayStart) && Objects.nonNull(todayEnd)) {
            lqw.between(MerchantTypeDO::getCreateTime, todayStart, todayEnd);
        }
        lqw.orderByDesc(MerchantTypeDO::getCreateTime);
        lqw.last("LIMIT " + 1);
        return merchantTypeMapper.selectOne(lqw);
    }

    @Override
    public Integer getMerchantTypeNo(String activeNo) {
        return merchantTypeMapper.getMerchantTypeNo(activeNo);
    }

}
